﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections.Specialized;
using System.Collections;

namespace Yuanlin.Business
{
    public class ProductAddControl : YuanlinUserControl
    {
        protected ValuedTextBox titleValuedTextBox;//商品名称（常用名）
        protected ValuedTextBox aliasValuedTextBox;//别名
        protected FileUpload attachmentData;//商品图片
        protected ValuedTextBox latinNameValuedTextBox;//拉丁名
        protected ValuedDropDownList categoryValuedDropDownList;//植物分类
        protected CheckBoxList applicationCategoryCheckBoxList;//应用分类
        protected IButton SaveButton;
        protected IButton BackButton;

        private int huamuFeedbackId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.HuamuFeedbackId);
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            Product product = new Product();
            
            product.Title.Value =  CensorWordManager.CensorWord(titleValuedTextBox.Value);
            product.Alias.Value =  CensorWordManager.CensorWord(aliasValuedTextBox.Value);
            product.QueryValue.Value = string.Format("、{0}、{1}", product.Title.Value, product.Alias.Value);
            product.LatinName.Value =  CensorWordManager.CensorWord(latinNameValuedTextBox.Value);
            product.CategoryId.Value = Int32.Parse(categoryValuedDropDownList.Value);
            product.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(attachmentData);

            if (BusinessManager.CreateProduct(product) == -2)
            {
                Globals.ShowMessage(this.Page, "添加失败","此信息已经存在！");
                return;
            }

            if (SiteUrls.Instance().IsAttachmentValid(attachmentData))
            {
                SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(product), attachmentData);
            }

            //创建花木与应用分类的关系
            SiteHuamuAndAppCategoryRelation siteHuamuAndAppCategoryRelation = new SiteHuamuAndAppCategoryRelation();
            siteHuamuAndAppCategoryRelation.EntityId.Value = 1;
            siteHuamuAndAppCategoryRelation.HuamuId.Value = product.EntityId.Value;
            foreach (ListItem item in this.applicationCategoryCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    siteHuamuAndAppCategoryRelation.CategoryIds += "," + Globals.ChangeType<int>(item.Value);
                }
            }
            if (!string.IsNullOrEmpty(siteHuamuAndAppCategoryRelation.CategoryIds))
            {
                siteHuamuAndAppCategoryRelation.CategoryIds = siteHuamuAndAppCategoryRelation.CategoryIds.Substring(1);
                BusinessManager.CreateSiteHuamuAndAppCategoryRelation(siteHuamuAndAppCategoryRelation);
            }

            CreateDefaultSection(product.EntityId.Value);

            PageRedirect();
        }
        private void CreateDataToDB(Sections sections, int plantId, int sectionId)
        {
            PlantSection_Section plantSection_Section = null;
            foreach (Section section in sections.Section)
            {
                plantSection_Section = new PlantSection_Section();
                plantSection_Section.Subject.Value = section.Name;
                plantSection_Section.Enabled.Value = 1;
                plantSection_Section.PlantId.Value = plantId;
                plantSection_Section.CategoryType.Value = categoryType;
                plantSection_Section.CategoryId.Value = sectionId;
                BusinessManager.CreatePlantSection_Section(plantSection_Section);
            }
        }
        //生成默认的7大版块
        private void CreateDefaultSection(int productId)
        {
            DefaultSection defaultSection = DefaultSectionManager.GetDefaultSection("Product");
            if (defaultSection == null)
            {
                return;
            }
            Sections sections = defaultSection.Sections["1"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 1);
            }

            sections = defaultSection.Sections["2"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 2);
            }

            sections = defaultSection.Sections["3"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 3);
            }

            sections = defaultSection.Sections["4"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 4);
            }

            sections = defaultSection.Sections["5"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 5);
            }

            sections = defaultSection.Sections["6"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 6);
            }
            sections = defaultSection.Sections["7"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, productId, 7);
            }
        }
        private void Bind()
        {
            //植物分类
            ProductCategoryListRequest request = new ProductCategoryListRequest();
            request.CategoryType = 0;
            EntityList categories = BusinessManager.GetProductCategoryList(RequestHelper.BuildGetProductCategoryListRequest(request));

            ProductCategory topCategory = new ProductCategory();
            topCategory.Title.Value = "<选择植物分类>";
            topCategory.EntityId.Value = -1;
            categories.Insert(0, topCategory);

            this.categoryValuedDropDownList.DataSource = categories;
            this.categoryValuedDropDownList.DataTextField = "Title";
            this.categoryValuedDropDownList.DataValueField = "EntityId";
            this.categoryValuedDropDownList.DataBind();

            //应用分类
            request = new ProductCategoryListRequest();
            request.CategoryType = 1;
            EntityList applicationCategories = BusinessManager.GetProductCategoryList(RequestHelper.BuildGetProductCategoryListRequest(request));
            this.applicationCategoryCheckBoxList.DataSource = applicationCategories;
            this.applicationCategoryCheckBoxList.DataTextField = "Title";
            this.applicationCategoryCheckBoxList.DataValueField = "EntityId";
            this.applicationCategoryCheckBoxList.DataBind();

            //反馈页面过来的就文本框不能修改
            if (huamuFeedbackId > 0)
            {
                titleValuedTextBox.Text = BusinessManager.GetHuaMuFeedback(huamuFeedbackId).Title.Value;
                titleValuedTextBox.Enabled = false;
            }
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetProductListUrl(string.Empty, string.Empty));
        }
    }
}

